package cn.piesat.controller;

import cn.piesat.dto.AgreementDTO;
import cn.piesat.service.intf.AgreementService;
import cn.piesat.util.ResponseWrapperUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 协议信息控制层
 * @author 奚好雷
 *
 */
@RestController
@RequestMapping("/manager/agreement")
public class AgreementController {
	
	@Autowired
	private AgreementService agreementService;
	
	/**
	 * 添加协议信息 默认使用
	 * @param json
	 * {
	 *      "title":"",协议标题
	 * 		"content":"",协议内容
	 * 		"protocolType":"",registered,注册协议,privacy,隐私协议
	 * }
	 * @return
	 */
	@PostMapping("/add")
	public String add(@RequestBody String json) {
		if(StringUtils.isBlank(json)) {
			return ResponseWrapperUtil.executeCustomTips("false", "参数不能为空");
		}
		AgreementDTO dto = JSON.parseObject(json, AgreementDTO.class);
		dto.setUpdateTime(new Date());
		dto.setEnableState(1);
		try {
			List<AgreementDTO> dtos = agreementService.findByProtocolType(dto.getProtocolType());
			if(!dtos.isEmpty()) {
				return ResponseWrapperUtil.executeCustomTips("false", "该协议配置已经存在,不允许重复添加");
			}
			agreementService.add(dto);
		} catch (Exception e) {
			return ResponseWrapperUtil.executeCustomTips("false", "添加失败");
		}
		return ResponseWrapperUtil.executeCustomTips("true", "添加成功");
	}
	 
	/**
	 * 修改协议信息
	 * @param json
	 * {
	 * 		"id":"",
	 *      "title":"",协议标题
	 * 		"content":"",协议内容
	 * 		"protocolType":"",registered,注册协议,privacy,隐私协议
	 * 		"enableState": 0 启用 1 禁用
	 * }
	 * @return
	 */
	@PostMapping("/update")
	public String update(@RequestBody String json) {
		if(StringUtils.isBlank(json)) {
			return ResponseWrapperUtil.executeCustomTips("false", "参数不能为空");
		}
		AgreementDTO dto = JSON.parseObject(json, AgreementDTO.class);
		dto.setUpdateTime(new Date());
		try {
			agreementService.update(dto);
		} catch (Exception e) {
			return ResponseWrapperUtil.executeCustomTips("false", "修改失败");
		}
		return ResponseWrapperUtil.executeCustomTips("true", "修改成功");
	}
	
	/**
	 * 	查询已经启用的
	 * @return
	 */
	@GetMapping("/get/{enableState}")
	public String getAllByEnableState(@PathVariable("enableState") Integer enableState) {
		List<AgreementDTO> list = agreementService.findByState(enableState);
		return ResponseWrapperUtil.executeResultList(list);
	}
	
	/**
	 * 	查询协议信息列表
	 * @param page 页数
	 * @param size 每页数量
	 * @return
	 */
	@GetMapping("/getAll/{page}/{size}")
	public String getAll(@PathVariable("page")Integer page,
						 @PathVariable("size")Integer size) {
		if (page < 0) {
			page = 0;
		}
		if (size <= 0) {
			size = 8;
		}
		Map<String, Object> resultMap = agreementService.findAll(page,size);
		return ResponseWrapperUtil.executeResultMap(resultMap);
	}
	
	/**
	 * 	删除指定ID信息
	 */
	@DeleteMapping("/del/{id}")
	public String del(@PathVariable("id")String id) {
		try {
			agreementService.del(id);
		} catch (Exception e) {
			return ResponseWrapperUtil.executeCustomTips("false", "删除失败");
		}
		return ResponseWrapperUtil.executeCustomTips("true", "删除成功");
	}

	/**
	 * 根据ID查询详细信息
	 * @param id
	 * @return
	 */
	@GetMapping("/find/{id}")
	public String findById(@PathVariable("id")String id){
		AgreementDTO dto = agreementService.findById(id);
		return ResponseWrapperUtil.executeResultEntity(dto);
	}
}
